<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
  <title>USDT 虚拟钱包 Demo</title>
  <style>
    body { font-family: sans-serif; padding: 20px; max-width: 400px; margin: auto; }
    .card { border: 1px solid #ddd; border-radius: 8px; padding: 16px; }
    .row { margin-bottom: 12px; }
    .label { color: #666; }
    .value { font-size: 1.2em; }
    button { width: 100%; padding: 10px; border:none; background:#4A5568; color:#fff; border-radius:4px; font-size:1em; }
    button:disabled { background:#aaa; }
    #msg { margin-top:12px; color:#c00; }
  </style>
</head>
<body>
  <div class="card">
    <div class="row">
      <div class="label">钱包地址：</div>
      <div id="addr" class="value">未连接</div>
    </div>
    <div class="row">
      <div class="label">账户余额：</div>
      <div id="bal" class="value">50,000,000,000 USDT</div>
    </div>
    <div class="row">
      <div class="label">本次可提：</div>
      <div id="lim" class="value">50 USDT</div>
    </div>
    <div class="row">
      <div class="label">下次可提时间：</div>
      <div id="next" class="value">--</div>
    </div>
    <button id="btn" disabled>连接钱包</button>
    <div id="msg"></div>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/tronweb/dist/TronWeb.js"></script>
  <script>
    // —— 合约相关配置 ——  
    const CONTRACT = 'TJc1U7hXk5aGZ7f1KXyvQ1rM2p8Y4LmYtY';  // 你部署后的合约地址  
    const ABI = [
      {
        "inputs": [{ "internalType": "address","name":"_usdt","type":"address" }],
        "stateMutability":"nonpayable","type":"constructor"
      },
      {
        "anonymous":false,
        "inputs":[
          { "indexed":true,"internalType":"address","name":"user","type":"address" },
          { "indexed":false,"internalType":"uint256","name":"amount","type":"uint256" }
        ],
        "name":"Withdraw","type":"event"
      },
      {
        "anonymous":false,
        "inputs":[
          { "indexed":false,"internalType":"uint256","name":"amount","type":"uint256" }
        ],
        "name":"Funded","type":"event"
      },
      {
        "inputs":[
          { "internalType":"address","name":"_user","type":"address" },
          { "internalType":"uint256","name":"_virtualBalance","type":"uint256" }
        ],
        "name":"setUser","outputs":[],"stateMutability":"nonpayable","type":"function"
      },
      {
        "inputs":[{ "internalType":"uint256","name":"amount","type":"uint256" }],
        "name":"fundContract","outputs":[],"stateMutability":"nonpayable","type":"function"
      },
      {
        "inputs":[],"name":"withdraw","outputs":[],"stateMutability":"nonpayable","type":"function"
      },
      {
        "inputs":[{ "internalType":"address","name":"_user","type":"address" }],
        "name":"getNextWithdrawTime",
        "outputs":[{ "internalType":"uint256","name":"","type":"uint256" }],
        "stateMutability":"view","type":"function"
      },
      {
        "inputs":[{ "internalType":"address","name":"_user","type":"address" }],
        "name":"getRemainingVirtualBalance",
        "outputs":[{ "internalType":"uint256","name":"","type":"uint256" }],
        "stateMutability":"view","type":"function"
      }
    ];

    let tronWeb, contract;

    async function init() {
      if (window.tronWeb && window.tronWeb.ready) {
        tronWeb = window.tronWeb;
        contract = await tronWeb.contract(ABI, CONTRACT);
        const addr = tronWeb.defaultAddress.base58;
        document.getElementById('addr').innerText = addr;
        document.getElementById('btn').innerText = '提现 50 USDT';
        document.getElementById('btn').disabled = false;
        loadNextTime(addr);
      } else {
        document.getElementById('msg').innerText = '请先安装并登录 TronLink 插件／App';
      }
    }

    async function loadNextTime(addr) {
      try {
        const t = await contract.getNextWithdrawTime(addr).call();
        const date = new Date(t * 1000);
        document.getElementById('next').innerText = date.toISOString().slice(0,10);
      } catch {
        document.getElementById('next').innerText = '--';
      }
    }

    async function withdraw() {
      document.getElementById('btn').disabled = true;
      document.getElementById('msg').innerText = '请求中…';
      try {
        await contract.withdraw().send();
        document.getElementById('msg').innerText = '提现交易已发起，请在 TronLink 中确认';
      } catch (e) {
        document.getElementById('msg').innerText = '提现失败：合约余额不足或周期未到';
      }
      document.getElementById('btn').disabled = false;
    }

    document.getElementById('btn').addEventListener('click', withdraw);
    window.addEventListener('load', init);
  </script>
</body>
</html>
